﻿import mx.data.types.Str;

dynamic class SODataProvider
{
	private var _dataname:String;
	private var _so:SharedObject;
	private var _localPath:String;
	
	
	public function get LocalPath():String {
		return _localPath;
		
	}
	public function set LocalPath(value:String) {
		_localPath = value;
		if(_localPath==null){
			_so = SharedObject.getLocal(value);
		}else {
			_so = SharedObject.getLocal(value,localPath);
		}
	}
	public function get SO():SharedObject
	{
		return _so;
	}
	public function get DataName():String
	{
		return _dataname;
	}
	public function set DataName(value:String)
	{
		if (value == null)
		{
			_so = null;
			_dataname = value;
		}else
		{
			_dataname = value;
			if(_localPath==null){
				_so = SharedObject.getLocal(value);
			}else {
				_so = SharedObject.getLocal(value,localPath);
			}
			checkSO();
		}
	}
	public function SODataProvider(name:String,localPath:String)
	{
		if (name == "" || name == undefined)
		{
			name = "defaultSO";
		}
		_dataname = name;
		_localPath = localPath;
		if(_localPath==null){
			_so = SharedObject.getLocal(value);
		}else {
			_so = SharedObject.getLocal(value,localPath);
		}
	}
	public function clear(){
		if (checkSO())
		{
			_so.clear();
		}
	}
	public function getDatas(fieldsToFull:Object):Object
	{
		if (!checkSO())
		{
			return null;
		}
		var newObej:Object = new Object();
		if (fieldsToFull == undefined)
		{
			for (var i:String in _so.data)
			{
				newObej[i] = _so.data[i];
			}
		}else{
			for (var i:String in fieldsToFull)
			{
				newObej[i] = _so.data[i];
			}
		}
		return newObej;
	}
	public function setDatas(datas:Object):Boolean
	{
		if (!checkSO())
		{
			return false;
		}
		for (var i:String in datas)
		{
			_so.data[i] = datas[i];
		}
		_so.flush();
		return true;
	}
	private function checkSO():Boolean
	{
		if (_so == null)
		{
			trace("由于安全，或其它问题，不能获取存取空间。");
			return false;
		}
		return true;
	}
}